package com.ptrsystem.siatd.dao.implementacion;

import com.ptrsystem.siatd.beans.SedeOrganizacional;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author uotu
 */
@Repository
public class mantenimientoSedeOrganImplement implements com.ptrsystem.siatd.dao.interfaces.mantenimientoSedeOrganizInterface {

    @Autowired
    private DataSource dataSource;

    @Override
    @Transactional
    public boolean getRegistraSedeOrgan(SedeOrganizacional sedOrg) {
        boolean rt = false;
        JdbcTemplate jdbc = new JdbcTemplate(this.dataSource);
        int rs = jdbc.update("insert into Tb_SedOrganizacional(nombre) values(?)", sedOrg.getNombre());
        if (rs > 0) {
            rt = !rt;
        }
        return rt;
    }

    @Override
    public List<SedeOrganizacional> getListaSedeOrgan() {
        JdbcTemplate jdbc = new JdbcTemplate(this.dataSource);
        List<SedeOrganizacional> lista = jdbc.query("select * from Tb_SedOrganizacional", new rowMapper());
        return lista;
    }

    public class rowMapper implements RowMapper<SedeOrganizacional> {

        @Override
        public SedeOrganizacional mapRow(ResultSet rs, int i) throws SQLException {
            SedeOrganizacional sedeOrg = new SedeOrganizacional(rs.getString(1),
                    rs.getString(2));
            return sedeOrg;
        }

    }

}
